﻿<UserControl x:Class="Peter.Sdw.View.VersionView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:logic="clr-namespace:Peter.Sdw.Logic"
             xmlns:icons="clr-namespace:Peter.Common.Icons;assembly=Peter.Common"
             xmlns:view="clr-namespace:Peter.Sdw.View"
             xmlns:controls="clr-namespace:Peter.Common.Controls;assembly=Peter.Common"
             mc:Ignorable="d"
             d:DesignHeight="600"
             d:DesignWidth="700"
             d:DataContext="{d:DesignInstance logic:VersionModel}">

   <UserControl.Resources>
      <logic:ChapterTypeVisibilityConverter x:Key="ChapterTypeVisibilityConverter" />
      <logic:SubBookTypeVisibilityConverter x:Key="SubBookTypeVisibilityConverter" />
      <DataTemplate DataType="{x:Type logic:SubBookModel}">
         <view:SubBookEditView />
      </DataTemplate>
      <DataTemplate DataType="{x:Type logic:ChapterModel}">
         <view:ChapterEditView />
      </DataTemplate>
   </UserControl.Resources>

   <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <Expander Grid.Row="0"
                IsExpanded="True"
                Header="Version Details">
         <Grid>
            <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="Auto" />
               <RowDefinition Height="Auto" />
               <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="*" />
               <ColumnDefinition Width="*" />
               <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="Title"
                       Grid.Row="0"
                       Grid.Column="0" />
            <TextBox Text="{Binding Path=Title}"
                     Grid.Row="1"
                     Grid.Column="0" />

            <TextBlock Text="Term"
                       Grid.Row="2"
                       Grid.Column="0" />
            <Grid Grid.Row="3"
                  Grid.Column="0">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="Auto" />
               </Grid.ColumnDefinitions>
               <TextBox Text="{Binding Path=TermName}"
                        Grid.Column="0"
                        IsReadOnly="True" />
               <Button Grid.Column="1"
                       Margin="5,0,0,0"
                       ToolTip="Select a Term"
                       CommandParameter="version"
                       Command="{Binding Path=SelectTermCommand}">
                  <icons:IconDisplay Icon="Browser"
                                     Margin="5,0" />
               </Button>
            </Grid>

            <TextBlock Text="Publish Date"
                       Grid.Row="0"
                       Grid.Column="1" />
            <TextBox Text="{Binding Path=PublishDate}"
                     Grid.Row="1"
                     Grid.Column="1" />

            <TextBlock Text="Source Name"
                       Grid.Row="2"
                       Grid.Column="1" />
            <TextBox Text="{Binding Path=SourceName}"
                     Grid.Row="3"
                     Grid.Column="1" />

            <TextBlock Text="Source Url"
                       Grid.Row="2"
                       Grid.Column="2" />
            <TextBox Text="{Binding Path=SourceUrl}"
                     Grid.Row="3"
                     Grid.Column="2" />

            <Button Grid.Row="0"
                    Grid.Column="2"
                    Style="{DynamicResource ButtonSuccess}"
                    Command="{Binding Path=AddSubBooksCommand}">
               <StackPanel Orientation="Horizontal">
                  <icons:IconDisplay Icon="Createfile"
                                     IconSize="Small" />
                  <TextBlock Text="Add SubBooks"
                             Margin="4,0"
                             VerticalAlignment="Center" />
               </StackPanel>
            </Button>
            <Button ToolTip="Set this version as the default version for this book"
                    Command="{Binding Path=SetAsDefaultCommand}"
                    Grid.Row="1"
                    Grid.Column="2">
               <StackPanel Orientation="Horizontal">
                  <icons:IconDisplay Icon="Certificatealt" />
                  <TextBlock Text="Default Version" />
               </StackPanel>
            </Button>

         </Grid>
      </Expander>

      <Grid Grid.Row="1">
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="1*" />
         </Grid.ColumnDefinitions>

         <Grid Grid.Column="0"
               Margin="5,0,0,0">
            <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="*" />
               <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Text="Contents"
                       FontWeight="Bold" />
            <TreeView Grid.Row="1"
                      x:Name="TreeContents"
                      ItemsSource="{Binding Path=SubBooks}">
               <TreeView.Resources>
                  <HierarchicalDataTemplate DataType="{x:Type logic:SubBookModel}"
                                            ItemsSource="{Binding Path=Children}">
                     <StackPanel Orientation="Horizontal">
                        <icons:IconDisplay Icon="Bookthree" />
                        <TextBlock Text="{Binding Path=TermName}"
                                   Margin="5,0,0,0" />
                     </StackPanel>
                  </HierarchicalDataTemplate>
                  <DataTemplate DataType="{x:Type logic:ChapterModel}">
                     <StackPanel Orientation="Horizontal">
                        <icons:IconDisplay Icon="Bookmarkthree" />
                        <TextBlock Text="{Binding Path=Name}"
                                   Margin="5,0,0,0" />
                     </StackPanel>
                  </DataTemplate>
               </TreeView.Resources>
            </TreeView>
            <Button Grid.Row="2"
                    Style="{DynamicResource ButtonSuccess}"
                    Command="{Binding ElementName=TreeContents, Path=SelectedItem.AddChaptersCommand}"
                    Visibility="{Binding ElementName=TreeContents, Path=SelectedItem, Converter={StaticResource SubBookTypeVisibilityConverter}}">
               <StackPanel Orientation="Horizontal">
                  <icons:IconDisplay Icon="Createfile"
                                     IconSize="Small" />
                  <TextBlock Text="Add Chapters"
                             Margin="4,0"
                             VerticalAlignment="Center" />
               </StackPanel>
            </Button>
         </Grid>

         <ContentPresenter Grid.Column="1"
                           Content="{Binding ElementName=TreeContents, Path=SelectedItem}" />

         <Grid Grid.Column="2"
               Margin="5,0,5,0"
               Visibility="{Binding ElementName=TreeContents, Path=SelectedItem, Converter={StaticResource ChapterTypeVisibilityConverter}}">
            <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="*" />
               <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Vertical">
               <TextBlock Text="Chapter Header" />
               <TextBox Height="60"
                        Text="{Binding ElementName=TreeContents, Path=SelectedItem.Header}"
                        GotFocus="OnChapterHeaderFocus"
                        VerticalScrollBarVisibility="Auto"
                        TextWrapping="WrapWithOverflow"
                        VerticalAlignment="Stretch"
                        AcceptsReturn="True" />
            </StackPanel>

            <ScrollViewer Grid.Row="1"
                          VerticalScrollBarVisibility="Auto"
                          HorizontalScrollBarVisibility="Disabled">
               <ItemsControl ItemsSource="{Binding ElementName=TreeContents, Path=SelectedItem.Passages}">
                  <ItemsControl.ItemsPanel>
                     <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical" />
                     </ItemsPanelTemplate>
                  </ItemsControl.ItemsPanel>
                  <ItemsControl.ItemTemplate>
                     <DataTemplate>
                        <Grid>
                           <Grid.RowDefinitions>
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                              <RowDefinition Height="Auto" />
                           </Grid.RowDefinitions>
                           <TextBlock Grid.Row="0"
                                      Text="Header" />
                           <TextBox Grid.Row="1"
                                    Text="{Binding Path=Header}"
                                    GotFocus="OnPassageHeaderGotFocus"/>
                           <TextBlock Grid.Row="2"
                                      Text="Entry" />
                           <TextBox Grid.Row="3"
                                    Height="60"
                                    x:Name="txt"
                                    Tag="{Binding}"
                                    Text="{Binding Path=Text}"
                                    GotFocus="OnPassageGotFocus"
                                    VerticalScrollBarVisibility="Auto"
                                    TextWrapping="WrapWithOverflow"
                                    VerticalAlignment="Stretch"
                                    AcceptsReturn="True" />
                           <Grid Grid.Row="4">
                              <Grid.ColumnDefinitions>
                                 <ColumnDefinition Width="*" />
                                 <ColumnDefinition Width="*" />
                              </Grid.ColumnDefinitions>
                              <Grid.RowDefinitions>
                                 <RowDefinition Height="Auto" />
                                 <RowDefinition Height="Auto" />
                              </Grid.RowDefinitions>
                              <TextBlock Text="Number"
                                         Grid.Row="0"
                                         Grid.Column="0"/>
                              <controls:NumericUpDown Grid.Column="0"
                                                      Precision="0"
                                                      Grid.Row="1"
                                                      Value="{Binding Path=Number}" />
                              <TextBlock Text="Order"
                                         Grid.Row="0"
                                         Grid.Column="1"/>
                              <controls:NumericUpDown Grid.Column="1"
                                                      Precision="0"
                                                      Grid.Row="1"
                                                      Value="{Binding Path=Order}" />
                           </Grid>
                           <GroupBox Header="New Footer"
                                     Grid.Row="5"
                                     Visibility="{Binding Path=ShowAddFooter, Converter={StaticResource _BoolToVisi}}">
                              <Grid>
                                 <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width=".6*" />
                                    <ColumnDefinition Width=".3*" />
                                 </Grid.ColumnDefinitions>
                                 <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                 </Grid.RowDefinitions>
                                 <TextBox Grid.Row="0"
                                          Grid.Column="0"
                                          Grid.ColumnSpan="2"
                                          Tag="{Binding}"
                                          Text="{Binding Path=NewFooterText}"
                                          VerticalScrollBarVisibility="Auto"
                                          TextWrapping="WrapWithOverflow"
                                          VerticalAlignment="Stretch"
                                          AcceptsReturn="True" />
                                 <controls:NumericUpDown Grid.Row="1"
                                                         Grid.Column="0"
                                                         Precision="0"
                                                         Value="{Binding Path=FooterIndex}" />
                                 <Button Grid.Row="1"
                                         Grid.Column="1">
                                    <icons:IconDisplay Icon="PlusSign"
                                                       IconSize="Small" />
                                 </Button>
                              </Grid>
                           </GroupBox>
                           <ItemsControl Grid.Row="6"
                                         ItemsSource="{Binding Path=Footers}">
                              <ItemsControl.ItemsPanel>
                                 <ItemsPanelTemplate>
                                    <StackPanel />
                                 </ItemsPanelTemplate>
                              </ItemsControl.ItemsPanel>
                              <ItemsControl.ItemTemplate>
                                 <DataTemplate>
                                    <Grid>
                                       <TextBox GotFocus="OnPassageFooterGotFocus"
                                                VerticalScrollBarVisibility="Auto"
                                                TextWrapping="WrapWithOverflow"
                                                VerticalAlignment="Stretch"
                                                AcceptsReturn="True" />
                                    </Grid>
                                 </DataTemplate>
                              </ItemsControl.ItemTemplate>
                           </ItemsControl>
                           <Border Grid.Row="7"
                                   HorizontalAlignment="Stretch"
                                   BorderBrush="SlateGray"
                                   BorderThickness="1"
                                   Margin="4"/>
                        </Grid>
                     </DataTemplate>
                  </ItemsControl.ItemTemplate>
               </ItemsControl>
            </ScrollViewer>

            <StackPanel Grid.Row="2"
                        Orientation="Vertical">
               <TextBlock Text="Chapter Footer" />
               <TextBox Height="60"
                        GotFocus="OnChapterFooterGotFocus"
                        Text="{Binding ElementName=TreeContents, Path=SelectedItem.Footer}"
                        VerticalScrollBarVisibility="Auto"
                        TextWrapping="WrapWithOverflow"
                        VerticalAlignment="Stretch"
                        AcceptsReturn="True" />
            </StackPanel>
         </Grid>

         <view:EditView Grid.Column="3"
                        x:Name="EditView"
                        Visibility="{Binding ElementName=TreeContents, Path=SelectedItem, Converter={StaticResource ChapterTypeVisibilityConverter}}" />

         <Border Grid.Row="0"
                 MaxWidth="150"
                 Grid.Column="1"
                 Grid.ColumnSpan="2"
                 Height="75"
                 CornerRadius="5"
                 Background="SteelBlue"
                 Visibility="{Binding ElementName=TreeContents, Path=SelectedItem.IsLoading, Converter={StaticResource _BoolToVisi}, FallbackValue=Collapsed}">
            <TextBlock Text="Loading..."
                       FontSize="16"
                       Foreground="White"
                       FontStyle="Italic"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
         </Border>
      </Grid>
   </Grid>
</UserControl>
